<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>ArduPilot Filter Analysis</title>
<link rel="icon" href="../images/AP_favicon.png">
<script type="text/javascript" src="filters.js"></script>
<script type="text/javascript" src="../Libraries/FileSaver.js"></script>
<script type="text/javascript" src="../Libraries/Array_Math.js"></script>
<script type="text/javascript" src="../Libraries/ParameterMetadata.js"></script>
<script type="text/javascript" src="../Libraries/Plotly_helpers.js"></script>

<script src='../modules/plotly.js/dist/plotly.min.js'></script>
</head>
<table style="width:1200px"><tr><td>
    <a href="https://ardupilot.org"><img src="../images/ArduPilot.png"></a>
</td><td>
    <a href="https://github.com/ArduPilot/WebTools"><img src="../images/github-mark.png" style="width:60px"></a>
    <br>
    <a href="https://github.com/ArduPilot/WebTools"><img src="../images/GitHub_Logo.png" style="width:60px"></a>
</td></tr></table>

<style>
        div.plotly-notifier {
                visibility: hidden;
        }
</style>

<h1><a href="" style="color: #000000; text-decoration:none;">ArduPilot Filter Analysis</a></h1>

The following form will display the attenuation and phase lag for an
ArduPilot 4.2 filter setup.
<body onload="load(); update_all_hidden(); calculate_filter(); calculate_pid();">
<div id="Bode" style="width:1200px;height:900px"></div>
<p>
  <input type="button" id="calculate" value="Calculate">
  <input type="button" id="SaveParams" value="Save Parameters" onclick="save_parameters();">
  <button class="styleClass" onclick="document.getElementById('param_file').click()">Load Parameters</button>
  <input type='file' id="param_file" style="display:none" onchange="load_parameters(this.files[0]);">
  <input type="button" id="GetLink" value="Get Link" onclick="get_link();">

  <form id="params" action="">
<fieldset style="max-width:1200px">
  <legend>Graph Settings</legend>
        <table>
                <tr>
                        <td>
                                <fieldset style="width:150px; height:70px">
                                        <legend>Magnitude scale</legend>
                                        <input type="radio" id="ScaleLog" name="Scale" value="Log" onchange="calculate_filter();" checked>
                                        <label for="ScaleLog">dB</label><br>
                                        <input type="radio" id="ScaleLinear" name="Scale" value="Linear" onchange="calculate_filter();">
                                        <label for="ScaleLinear">Linear</label><br>
                                </fieldset>
                        </td>
                        <td>
                                <fieldset style="width:150px; height:70px">
                                        <legend>Phase scale</legend>
                                        <input type="radio" id="ScaleUnWrap" name="PhaseScale" value="unwrap" onchange="calculate_filter();" checked>
                                        <label for="ScaleUnWrap">un-wrapped</label><br>
                                        <input type="radio" id="ScaleWrap" name="PhaseScale" value="wrap" onchange="calculate_filter();">
                                        <label for="ScaleWrap">±180</label><br>
                                </fieldset>
                        </td>
                        <td>
                                <fieldset style="width:150px; height:70px">
                                        <legend>Frequency scale</legend>
                                        <table>
                                                <tr>
                                                        <td>
                                                                <input type="radio" id="freq_ScaleLog" name="feq_scale" value="Log" onchange="calculate_filter();" checked>
                                                                <label for="freq_ScaleLog">Log</label><br>
                                                                <input type="radio" id="freq_ScaleLinear" name="feq_scale" value="Linear" onchange="calculate_filter();">
                                                                <label for="freq_ScaleLinear">Linear</label><br>
                                                        </td>
                                                        <td>
                                                                <input type="radio" id="freq_Scale_Hz" name="feq_unit" value="Hz" onchange="calculate_filter();" checked>
                                                                <label for="freq_Scale_Hz">Hz</label><br>
                                                                <input type="radio" id="freq_Scale_RPM" name="feq_unit" value="RPM" onchange="calculate_filter();">
                                                                <label for="freq_Scale_RPM">RPM</label><br>
                                                        </td>
                                                </tr>
                                        </table>
                                </fieldset>
                        </td>
                        <td>
                                <fieldset style="width:220px; height:70px">
                                        <legend>Options</legend>
                                        <input type="checkbox" id="ShowComponents" name="ShowComponents" onchange="calculate_filter()">
                                        <label for="ShowComponents">Show individual filters</label>
                                </fieldset>
                        </td>
                </tr>
        </table>
</fieldset>
<fieldset style="max-width:1200px">
  <legend>INS Settings</legend>
        <p>
                <label for="GyroSampleRate">Gyro Sample Rate</label>
                <input id="GyroSampleRate" name="GyroSampleRate" type="number" step="1" value="2000"/>
	</p>
        <p>
                <input id="INS_GYRO_FILTER" name="INS_GYRO_FILTER" type="number" step="0.1" value="20.0" style="width: 100px"/>
	</p>
</fieldset>
<table>
        <tr>
        <td>
        <fieldset style="width:580px">
            <legend>First Notch Filter</legend>
            <p>
                <input id="INS_HNTCH_ENABLE" name="INS_HNTCH_ENABLE" type="number" step="1" value="0" style="width: 100px" onchange="update_hidden(this.id)"/>
            </p>
            <p>
                <input id="INS_HNTCH_MODE" name="INS_HNTCH_MODE" type="number" step="1" value="0" style="width: 100px" onchange="update_hidden_mode()"/>
            </p>
            <p>
                <input id="INS_HNTCH_FREQ" name="INS_HNTCH_FREQ" type="number" step="0.1" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTCH_BW" name="INS_HNTCH_BW" type="number" step="0.1" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTCH_ATT" name="INS_HNTCH_ATT" type="number" step="0.1" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTCH_REF" name="INS_HNTCH_REF" type="number" step="0.01" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTCH_FM_RAT" name="INS_HNTCH_FM_RAT" type="number" step="0.01" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTCH_HMNCS" name="INS_HNTCH_HMNCS" type="number" step="1" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTCH_OPTS" name="INS_HNTCH_OPTS" type="number" step="1" value="0" style="width: 100px"/>
            </p>
        </fieldset>
        </td>
        <td>
        <fieldset style="width:580px">
            <legend>Second Notch Filter</legend>
            <p>
                <input id="INS_HNTC2_ENABLE" name="INS_HNTC2_ENABLE" type="number" step="1" value="0" style="width: 100px" onchange="update_hidden(this.id)"/>
            </p>
            <p>
                <input id="INS_HNTC2_MODE" name="INS_HNTC2_MODE" type="number" step="1" value="0" style="width: 100px" onchange="update_hidden_mode()"/>
            </p>
            <p>
                <input id="INS_HNTC2_FREQ" name="INS_HNTC2_FREQ" type="number" step="0.1" value="0"style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTC2_BW" name="INS_HNTC2_BW" type="number" step="0.1" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTC2_ATT" name="INS_HNTC2_ATT" type="number" step="0.1" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTC2_REF" name="INS_HNTC2_REF" type="number" step="0.01" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTC2_FM_RAT" name="INS_HNTC2_FM_RAT" type="number" step="0.01" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTC2_HMNCS" name="INS_HNTC2_HMNCS" type="number" step="1" value="0" style="width: 100px"/>
            </p>
            <p>
                <input id="INS_HNTC2_OPTS" name="INS_HNTC2_OPTS" type="number" step="1" value="0" style="width: 100px"/>
            </p>
        </fieldset>
        </td>
        </tr>
        </table>
<fieldset style="max-width:1200px" id="Throttle_input">
        <legend>Throttle Based</legend>
              <p>
                      <label for="Throttle">Throttle</label>
                      <input id="Throttle" name="Throttle" type="number" step="0.01" value="0.3"/>
              </p>
</fieldset>
<fieldset style="max-width:1200px" id="ESC_input">
        <legend>ESC Telemetry</legend>
              <p>
                      <label for="NUM_MOTORS">Number of Motors</label>
                      <input id="NUM_MOTORS" name="NUM_MOTORS" type="number" step="1" value="1"/>
              </p>
              <p>
                      <label for="ESC_RPM">ESC RPM</label>
                      <input id="ESC_RPM" name="ESC_RPM" type="number" step="1" value="2500"/>
              </p>
</fieldset>
<fieldset style="max-width:1200px" id="RPM_input">
        <legend>RPM/EFI Based</legend>
              <p>
                      <label for="RPM1">RPM1</label>
                      <input id="RPM1" name="RPM1" type="number" step="1" value="2500"/>
              </p>
              <p>
                      <label for="RPM2">RPM2</label>
                      <input id="RPM2" name="RPM2" type="number" step="1" value="2500"/>
              </p>
</fieldset>
</form>

<h2>PIDs</h2>
<h3><label id="PID_title">Title</label></h3>
<div id="BodePID" style="width:1200px;height:900px"></div>
<p>
<input type="button" id="CalculateRoll" value="Caculate Roll" onclick="calculate_pid(this.id);">
<input type="button" id="CalculatePitch" value="Caculate Pitch" onclick="calculate_pid(this.id);">
<input type="button" id="CalculateYaw" value="Caculate Yaw" onclick="calculate_pid(this.id);">
</p>
<form id="PID_params" action="">
        <fieldset style="max-width:1200px">
          <legend>Graph Settings</legend>
                <p>
                        <table>
                                <tr>
                                        <td>
                                                <fieldset style="width:150px; height:70px">
                                                        <legend>Gain scale</legend>
                                                        <input type="radio" id="PID_ScaleLog" name="PID_Scale" value="Log"  onchange="calculate_pid();" checked>
                                                        <label for="PID_ScaleLog">dB</label><br>
                                                        <input type="radio" id="PID_ScaleLinear" name="PID_Scale" value="Linear" onchange="calculate_pid();">
                                                        <label for="PID_ScaleLinear">Linear</label><br>
                                                </fieldset>
                                        </td>
                                        <td>
                                                <fieldset style="width:150px; height:70px">
                                                        <legend>Phase scale</legend>
                                                        <input type="radio" id="PID_ScaleUnWrap" name="PID_PhaseScale" value="unwrap" onchange="calculate_pid();" checked>
                                                        <label for="PID_ScaleUnWrap">un-wrapped</label><br>
                                                        <input type="radio" id="PID_ScaleWrap" name="PID_PhaseScale" value="wrap"onchange="calculate_pid();" >
                                                        <label for="PID_ScaleWrap">±180</label><br>
                                                </fieldset>
                                        </td>
                                        <td>
                                                <fieldset style="width:150px; height:70px">
                                                        <legend>Frequency scale</legend>
                                                        <table>
                                                                <tr>
                                                                        <td>
                                                                                <input type="radio" id="PID_freq_ScaleLog" name="PID_feq_scale" value="Log" onchange="calculate_pid();" checked>
                                                                                <label for="PID_freq_ScaleLog">Log</label><br>
                                                                                <input type="radio" id="PID_freq_ScaleLinear" name="PID_feq_scale" value="Linear" onchange="calculate_pid();">
                                                                                <label for="PID_freq_ScaleLinear">Linear</label><br>
                                                                        </td>
                                                                        <td>
                                                                                <input type="radio" id="PID_freq_Scale_Hz" name="PID_feq_unit" value="Hz" onchange="calculate_pid();" checked>
                                                                                <label for="PID_freq_Scale_Hz">Hz</label><br>
                                                                                <input type="radio" id="PID_freq_Scale_RPM" name="PID_feq_unit" value="RPM" onchange="calculate_pid();">
                                                                                <label for="PID_freq_Scale_RPM">RPM</label><br>
                                                                        </td>
                                                                </tr>
                                                        </table>
                                                </fieldset>
                                        </td>
                                        <td>
                                                <fieldset style="width:150px; height:70px">
                                                        <legend>Filtering</legend>
                                                        <input type="radio" id="PID_filtering_Pre" name="filtering" value="Pre" onchange="calculate_pid();" checked>
                                                        <label for="PID_filtering_Pre">Pre</label><br>
                                                        <input type="radio" id="PID_filtering_Post" name="filtering" value="Post" onchange="calculate_pid();">
                                                        <label for="PID_filtering_Post">Post</label><br>
                                                </fieldset>
                                        </td>
                                        <td>
                                                <fieldset style="width:220px; height:70px">
                                                        <legend>Options</legend>
                                                        <input type="checkbox" id="PID_ShowComponents" name="PID_ShowComponents" onchange="calculate_pid()">
                                                        <label for="PID_ShowComponents">Show individual components</label>
                                                </fieldset>
                                        </td>
                                </tr>
                        </table> 
                </p>
        </fieldset>
        <fieldset style="max-width:1200px">
        <legend>Loop Rate</legend>
               <p>
                        <input id="SCHED_LOOP_RATE" name="SCHED_LOOP_RATE" type="number" step="1" value="400" data-paramValues="false"/>
                </p>
        </fieldset>
        <fieldset style="max-width:1200px">
        <legend>Roll</legend>
                <p>
                        <input id="ATC_RAT_RLL_P" name="ATC_RAT_RLL_P" type="number" step="0.01" value="0.135"/>
                </p>
                <p>
                        <input id="ATC_RAT_RLL_I" name="ATC_RAT_RLL_I" type="number" step="0.01" value="0.135"/>
                </p>
                <p>
                        <input id="ATC_RAT_RLL_D" name="ATC_RAT_RLL_D" type="number" step="0.0001" value="0.0036"/>
                </p>
                <p>
                        <input id="ATC_RAT_RLL_FLTE" name="ATC_RAT_RLL_FLTE" type="number" step="0.01" value="0"/>
                </p>
                <p>
                        <input id="ATC_RAT_RLL_FLTD" name="ATC_RAT_RLL_FLTD" type="number" step="0.01" value="20"/>
                </p>
        </fieldset>
        <fieldset style="max-width:1200px">
        <legend>Pitch</legend>
                <p>
                        <input id="ATC_RAT_PIT_P" name="ATC_RAT_PIT_P" type="number" step="0.01" value="0.135"/>
                </p>
                <p>
                        <input id="ATC_RAT_PIT_I" name="ATC_RAT_PIT_I" type="number" step="0.01" value="0.135"/>
                </p>
                <p>
                        <input id="ATC_RAT_PIT_D" name="ATC_RAT_PIT_D" type="number" step="0.0001" value="0.0036"/>
                </p>
                <p>
                        <input id="ATC_RAT_PIT_FLTE" name="ATC_RAT_PIT_FLTE" type="number" step="0.01" value="0"/>
                </p>
                <p>
                        <input id="ATC_RAT_PIT_FLTD" name="ATC_RAT_PIT_FLTD" type="number" step="0.01" value="20"/>
                </p>
        </fieldset>
        <fieldset style="max-width:1200px">
        <legend>Yaw</legend>
                <p>
                        <input id="ATC_RAT_YAW_P" name="ATC_RAT_YAW_P" type="number" step="0.01" value="0.09"/>
                </p>
                <p>
                        <input id="ATC_RAT_YAW_I" name="ATC_RAT_YAW_I" type="number" step="0.01" value="0.009"/>
                </p>
                <p>
                        <input id="ATC_RAT_YAW_D" name="ATC_RAT_YAW_D" type="number" step="0.0001" value="0"/>
                </p>
                <p>
                        <input id="ATC_RAT_YAW_FLTE" name="ATC_RAT_YAW_FLTE" type="number" step="0.01" value="2.5"/>
                </p>
                <p>
                        <input id="ATC_RAT_YAW_FLTD" name="ATC_RAT_YAW_FLTD" type="number" step="0.01" value="0"/>
                </p>
        </fieldset>
</form>

<script>

  window.onerror = function(msg, url, linenumber) {
      alert('Sorry, something went wrong.\n\n' + 
            'Please try a hard reload of this page to clear its cache.\n\n' +
            'If the error persists open an issue on the GitHub repo.\n' +
            'Include a copy of the log and the following error message:\n\n' +
             msg + '\n' +
            'URL: '+ url +'\n' +
            'Line Number: '+ linenumber)
      return false
  }
  window.addEventListener('unhandledrejection', function (e) {
        throw new Error(e.reason.stack)
  })

  var calc_btn = document.getElementById('calculate');
  calc_btn.onclick = function() {
      calculate_filter();
  }
  //var clear_btn = document.getElementById('clear_cookies');
  //clear_btn.onclick = function() {
  //    clear_cookies();
  //}

  let params = ["SCHED_LOOP_RATE"]
  var inputs = document.getElementsByTagName("input");
  for (param of inputs) {
      if (param.id.startsWith("INS_") || param.id.startsWith("ATC_")) {
          params.push(param.id)
      }
  }

  load_param_inputs("params.json", params)


</script>

</body>
</html>
